﻿using ProjectFootball.MVC.Areas.MatchArea.Models;
using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using ProjectFootball.MVC.Models.RepositoryProviders;
using ProjectFootball.MVC.Services.Import;
using System;
using System.Linq;
using System.Web.Mvc;

namespace ProjectFootball.MVC.Areas.MatchArea.Controllers
{
    [Authorize(Roles = "Admin")]
    public class ImportController : BaseController
    {
        #region Constructor
        public ImportController(IRepositoryProvider provider)
            :base(provider)
        {
        }
        #endregion

        #region MatchArea/Import
        public ActionResult Index()
        {
            return View(new MatchImportViewModel());
        }

        [HttpPost]
        public ActionResult Index(MatchImportViewModel matchImportViewModel)
        {
            var espn = new Espn(Provider, User);
            espn.ImportMatches(matchImportViewModel.StartDate, matchImportViewModel.EndDate, matchImportViewModel.CompetitionGuid);

            return View(matchImportViewModel);
        }
        #endregion

        #region MatchArea/Import/FullMatch
        public ActionResult FullMatch(Guid? id)
        {
            if (id != null)
            {
                Espn espn = new Espn(Provider, User);
                espn.ImportMatchDetail((Guid)id);
            }

            var matches = Provider.LookupMatchRepository.Where(m => m.Match.MatchDate <= DateTime.Now && m.ImportStatus != ImportStatus.FullDetails).Select(m => m.Match).AsEnumerable();

            return View(matches.Search(User, SearchType.Import));

        }
        #endregion

        [OutputCache(Duration = 0)]
        public JsonResult GetCompetitionImportCodePickerData()
        {
            return Json(Provider.LookupCompetitionRepository.GetImportCodePickerData(), JsonRequestBehavior.AllowGet);
        }
    }
}
